/*
 * Copyright (c) 2018-2999 九五云信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 */

package com.yami.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.model.ShopWithdrawCash;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.dao.ShopWalletMapper;
import com.yami.shop.bean.model.ShopWallet;
import com.yami.shop.service.ShopWalletLogService;
import com.yami.shop.service.ShopWalletService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;

/**
 * 商家钱包信息
 *
 * @author Dwl
 * @date 2019-09-19 14:02:57
 */
@Service
@AllArgsConstructor
public class ShopWalletServiceImpl extends ServiceImpl<ShopWalletMapper, ShopWallet> implements ShopWalletService {

    private final ShopWalletMapper shopWalletMapper;

    @Autowired
    private ShopWalletLogService shopWalletLogService;

    @Override
    public ShopWallet getShopWalletByShopId(Long shopId) {
        return shopWalletMapper.selectOne(new LambdaQueryWrapper<ShopWallet>().eq(ShopWallet::getShopId,shopId));
    }

    @Override
    public IPage<ShopWallet> pageShopWallet(PageParam<ShopWallet> page, ShopWallet shopWallet) {
        return shopWalletMapper.pageShopWallet(page, shopWallet);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseEntity<Void> createShopWallet(Long shopId) {
        if (shopId == null) {
            throw new YamiShopBindException("店铺ID不能为空");
        }
        
        // 检查是否已存在
        ShopWallet existingWallet = lambdaQuery().eq(ShopWallet::getShopId, shopId).one();
        if (existingWallet != null) {
            throw new YamiShopBindException("店铺钱包已存在");
        }
        
        // 创建新钱包
        ShopWallet shopWallet = new ShopWallet();
        shopWallet.setShopId(shopId);
        shopWallet.setDepositAmount(BigDecimal.ZERO);
        shopWallet.setOperationAmount(BigDecimal.ZERO);
        shopWallet.setRefreshPoints(0);
        shopWallet.setUpdateTime(new Date());
        
        save(shopWallet);
        
        return ResponseEntity.ok().build();
    }

    @Override
    public ResponseEntity<ShopWallet> getShopWallet(Long shopId) {
        if (shopId == null) {
            throw new YamiShopBindException("店铺ID不能为空");
        }
        
        ShopWallet shopWallet = lambdaQuery().eq(ShopWallet::getShopId, shopId).one();
        if (shopWallet == null) {
            throw new YamiShopBindException("店铺钱包不存在");
        }
        
        return ResponseEntity.ok(shopWallet);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseEntity<Void> increaseDepositAmount(Long shopId, BigDecimal amount, String remark) {
        if (shopId == null) {
            throw new YamiShopBindException("店铺ID不能为空");
        }
        
        if (amount == null || amount.compareTo(BigDecimal.ZERO) <= 0) {
            throw new YamiShopBindException("金额必须大于0");
        }
        
        // 增加保证金
        int rows = baseMapper.increaseDepositAmount(shopId, amount);
        if (rows == 0) {
            throw new YamiShopBindException("增加保证金失败，店铺钱包不存在");
        }
        
//         记录交易
//        shopWalletLogService.saveShopWalletLog(shopId, 5, amount, "保证金增加", remark);
        
        return ResponseEntity.ok().build();
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseEntity<Void> decreaseDepositAmount(Long shopId, BigDecimal amount, String remark) {
        if (shopId == null) {
            throw new YamiShopBindException("店铺ID不能为空");
        }
        
        if (amount == null || amount.compareTo(BigDecimal.ZERO) <= 0) {
            throw new YamiShopBindException("金额必须大于0");
        }
        
        // 检查保证金是否足够
        ShopWallet shopWallet = lambdaQuery().eq(ShopWallet::getShopId, shopId).one();
        if (shopWallet == null) {
            throw new YamiShopBindException("店铺钱包不存在");
        }
        
        if (shopWallet.getDepositAmount().compareTo(amount) < 0) {
            throw new YamiShopBindException("保证金不足");
        }
        
        // 减少保证金
        int rows = baseMapper.decreaseDepositAmount(shopId, amount);
        if (rows == 0) {
            throw new YamiShopBindException("减少保证金失败");
        }
        
        // 记录交易
//        shopWalletRecordService.createRecord(shopId, 6, amount, "保证金减少", remark);
        
        return ResponseEntity.ok().build();
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseEntity<Void> increaseOperationAmount(Long shopId, BigDecimal amount, String remark) {
        if (shopId == null) {
            throw new YamiShopBindException("店铺ID不能为空");
        }
        
        if (amount == null || amount.compareTo(BigDecimal.ZERO) <= 0) {
            throw new YamiShopBindException("金额必须大于0");
        }
        
        // 增加运营金额
        int rows = baseMapper.increaseOperationAmount(shopId, amount);
        if (rows == 0) {
            throw new YamiShopBindException("增加运营金额失败，店铺钱包不存在");
        }
        
        // 记录交易
//        shopWalletRecordService.createRecord(shopId, 7, amount, "运营金额增加", remark);
        
        return ResponseEntity.ok().build();
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseEntity<Void> decreaseOperationAmount(Long shopId, BigDecimal amount, String remark) {
        if (shopId == null) {
            throw new YamiShopBindException("店铺ID不能为空");
        }
        
        if (amount == null || amount.compareTo(BigDecimal.ZERO) <= 0) {
            throw new YamiShopBindException("金额必须大于0");
        }
        
        // 检查运营金额是否足够
        ShopWallet shopWallet = lambdaQuery().eq(ShopWallet::getShopId, shopId).one();
        if (shopWallet == null) {
            throw new YamiShopBindException("店铺钱包不存在");
        }
        
        if (shopWallet.getOperationAmount().compareTo(amount) < 0) {
            throw new YamiShopBindException("运营金额不足");
        }
        
        // 减少运营金额
        int rows = baseMapper.decreaseOperationAmount(shopId, amount);
        if (rows == 0) {
            throw new YamiShopBindException("减少运营金额失败");
        }
        
        // 记录交易
//        shopWalletRecordService.createRecord(shopId, 8, amount, "运营金额减少", remark);
        
        return ResponseEntity.ok().build();
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseEntity<Void> increaseRefreshPoints(Long shopId, Integer points, String remark) {
        if (shopId == null) {
            throw new YamiShopBindException("店铺ID不能为空");
        }
        
        if (points == null || points <= 0) {
            throw new YamiShopBindException("点数必须大于0");
        }
        
        // 增加刷新点
        int rows = baseMapper.increaseRefreshPoints(shopId, points);
        if (rows == 0) {
            throw new YamiShopBindException("增加刷新点失败");
        }
        
        // 记录交易
//        shopWalletRecordService.createRecord(shopId, 9, points, "刷新点增加", remark);
        
        return ResponseEntity.ok().build();
    }

    @Override
    public ResponseEntity<Void> decreaseRefreshPoints(Long shopId, Integer points, String remark) {
        if (shopId == null) {
            throw new YamiShopBindException("店铺ID不能为空");
        }
        
        if (points == null || points <= 0) {
            throw new YamiShopBindException("点数必须大于0");
        }
        
        // 检查刷新点是否足够
        ShopWallet shopWallet = lambdaQuery().eq(ShopWallet::getShopId, shopId).one();
        if (shopWallet == null) {
            throw new YamiShopBindException("店铺钱包不存在");
        }
        
        if (shopWallet.getRefreshPoints() < points) {
            throw new YamiShopBindException("刷新点不足");
        }
        
        // 减少刷新点
        int rows = baseMapper.decreaseRefreshPoints(shopId, points);
        if (rows == 0) {
            throw new YamiShopBindException("减少刷新点失败");
        }
        // 记录交易
//        shopWalletRecordService.createRecord(shopId, 10, points, "刷新点减少", remark);
        
        return ResponseEntity.ok().build();
    }

}
